<?php


namespace app\mingteng\controller;
use think\AjaxPage;
use think\Db;

class Medal extends Base
{

    public $listRows = 20;

    public function index(){
        $medal = Db::name('medal_wall')->where('medalWallDel', '=', '0')->select();
        $this->assign('medal', $medal);

        return $this->fetch();
    }

    public function ajax_index(){
        $params = input('post.');
        $page = input('page', '1');

        $where = array();
        $where['medalWallDel'] = array('=', '0');
        if (isset($params['keywords']) && !empty($params['keywords'])) {
            $where['medalName'] = array('like', '%'.$params['keywords'].'%');
        }
        $count = Db::name('medal_wall')->where($where)->count();
        $list = Db::name('medal_wall')
            ->where($where)
            ->page($page, $this->listRows)
            ->select();

        foreach($list as $k=>$v){
            $list[$k]['addTime'] = date('Y-m-d H:i',$v['addTime']);
        }
        $this->assign('list', $list);

        $ajaxPage = new AjaxPage($count, $this->listRows);
        $show = $ajaxPage->show();
        $this->assign('page', $show);

        return $this->fetch();
    }

    public function add(){

        return $this->fetch();
    }

    public function doAdd(){
        $params = input('post.');

        if (empty($params['medalName'])) {
            return json(array('code' => 500, 'msg' => '请输入勋章名称'));
        }
        if (empty($params['activationMedaImage'])) {
            return json(array('code' => 500, 'msg' => '请选择激活图片'));
        }
        if (empty($params['noActivationMedaImage'])) {
            return json(array('code' => 500, 'msg' => '请选择未激活图片'));
        }

        $catch = array(
            'medalName' => $params['medalName'],
            'activationMedaImage' => $params['activationMedaImage'],
            'noActivationMedaImage' => $params['noActivationMedaImage'],
            'addTime' => time(),
        );
        Db::name('medal_wall')->insertGetId($catch);
        return json(array('code' => 200, 'msg' => '添加成功'));
    }

    public function edit(){
        $medalWallId = input('medalWallId');

        $detail = Db::name('medal_wall')->where('medalWallId', '=', $medalWallId)->find();
        $this->assign('detail', $detail);
        $this->assign('medalWallId', $medalWallId);

        return $this->fetch();
    }

    public function doEdit(){
        $params = input('post.');

        if (empty($params['medalName'])) {
            return json(array('code' => 500, 'msg' => '请输入勋章名称'));
        }
        if (empty($params['activationMedaImage'])) {
            return json(array('code' => 500, 'msg' => '请选择激活图片'));
        }
        if (empty($params['noActivationMedaImage'])) {
            return json(array('code' => 500, 'msg' => '请选择未激活图片'));
        }

        $catch = array(
            'medalName' => $params['medalName'],
            'activationMedaImage' => $params['activationMedaImage'],
            'noActivationMedaImage' => $params['noActivationMedaImage'],
        );
        Db::name('medal_wall')->where('medalWallId', '=', $params['medalWallId'])->save($catch);
        return json(array('code' => 200, 'msg' => '编辑成功'));
    }

    public function del(){
        $del_id = input('del_id');

        Db::name('medal_wall')->where('medalWallId', '=', $del_id)->save(array(
            'medalWallDel' => '1',
        ));
        return json(array('code' => 200, 'msg' => '操作成功'));
    }

}